package com.cn.serverline.lineicp.productionLine.service.impl;

import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.cn.serverline.lineicp.productionLine.constants.ProcessMaterialsConstant;
import com.cn.serverline.lineicp.productionLine.entity.ProcessMaterials;
import com.cn.serverline.lineicp.productionLine.entity.ProductionProcesses;
import com.cn.serverline.lineicp.productionLine.enums.ProcessMaterialTypeEnum;
import com.cn.serverline.lineicp.productionLine.enums.ProcessMaterialsException;
import com.cn.serverline.lineicp.productionLine.factory.MaterialHandlerFactory;
import com.cn.serverline.lineicp.productionLine.handler.MaterialHandler;
import com.cn.serverline.lineicp.productionLine.mapper.ProcessMaterialsMapper;
import com.cn.serverline.lineicp.productionLine.mapper.ProductionProcessesMapper;
import com.cn.serverline.lineicp.productionLine.param.ProcessMaterialsAddParam;
import com.cn.serverline.lineicp.productionLine.param.ProcessMaterialsEditParam;
import com.cn.serverline.lineicp.productionLine.param.ProcessMaterialsIdParam;
import com.cn.serverline.lineicp.productionLine.param.ProcessMaterialsPageParam;
import com.cn.serverline.lineicp.productionLine.service.IProcessMaterialsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.productionLine.util.DictUtil;
import com.cn.serverline.lineicp.workshopPlan.entity.AuxiliaryMaterials;
import com.cn.serverline.lineicp.workshopPlan.entity.SparePartsType;
import com.cn.serverline.lineicp.workshopPlan.entity.TypesOfConsumables;
import com.cn.serverline.lineicp.workshopPlan.mapper.AuxiliaryMaterialsMapper;
import com.cn.serverline.lineicp.workshopPlan.mapper.SparePartsTypeMapper;
import com.cn.serverline.lineicp.workshopPlan.mapper.TypesOfConsumablesMapper;
import org.springframework.stereotype.Service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;

import javax.annotation.Resource;

import static com.sun.jna.platform.mac.IOReturnException.getCode;


/**
 * <p>
 * 工序物料 服务实现类
 * </p>
 *
 * @author Gmr
 * @since 2024-07-31
 */
@Service("ProcessMaterialsServiceImpl")
public class ProcessMaterialsServiceImpl extends ServiceImpl<ProcessMaterialsMapper, ProcessMaterials> implements IProcessMaterialsService {

    // 工序Mapper
    @Resource
    private ProductionProcessesMapper productionProcessesMapper;

    // 字典工具
    @Resource
    private DictUtil dictUtil;

    /**
     * 新增
     * @param addParam 请求参数
     */
    @Override
    public void add(ProcessMaterialsAddParam addParam) {

        // 判断工序是否存在
        getProductionProcesses(addParam.getProductionProcessesId());

        // 判断物料类型是否存在
        if(!ProcessMaterialTypeEnum.validateType(addParam.getMaterialType())) {
            throw new ServiceException(ProcessMaterialsException.MATERIAL_TYPE_NON_EXISTENT);
        }

        // 判断数量单位是否存在
        checkNumberUnitTypeExist(addParam.getQuantityUnit());

        ProcessMaterials processMaterials = BeanUtil.copyProperties(addParam, ProcessMaterials.class);
        MaterialHandler handler = MaterialHandlerFactory.getHandler(addParam.getMaterialType());

        // 根据物料代号是否存在判断是否需要新增
        if(!handler.validateMaterial(addParam.getCode())) {
            handler.saveMaterial(processMaterials);
        } else {
            processMaterials.setMaterialTypeId(handler.getMaterialTypeId(addParam.getCode()));
        }

        this.save(processMaterials);
    }

    /**
     * 删除
     * @param idParam 请求参数
     */
    @Override
    public void del(ProcessMaterialsIdParam idParam) {
        ProcessMaterials processMaterials = getInfo(idParam.getId());
        this.removeById(processMaterials);
    }

    /**
     * 编辑
     * @param editParam 请求参数
     */
    @Override
    public void edit(ProcessMaterialsEditParam editParam) {
        
        // 获取信息
        ProcessMaterials materials = getInfo(editParam.getId());

        // 判断数量单位是否存在
        checkNumberUnitTypeExist(editParam.getQuantityUnit());

        BeanUtil.copyProperties(editParam, materials);
        MaterialHandler handler = MaterialHandlerFactory.getHandler(materials.getMaterialType());

        // 根据物料代号是否存在判断是否需要新增
        if(!handler.validateMaterial(editParam.getCode())) {
            handler.saveMaterial(materials);
        } else {
            materials.setMaterialTypeId(handler.getMaterialTypeId(editParam.getCode()));
        }
        
        this.updateById(materials);
    }

    /**
     * 验证数量单位是否存在
     * @param quantityUnit 数量单位
     */
    private void checkNumberUnitTypeExist(String quantityUnit) {
        if(!dictUtil.validateDict(ProcessMaterialsConstant.NUMBER_UNIT_TYPE, quantityUnit)) {
            throw new ServiceException(ProcessMaterialsException.NUMBER_UNIT_NON_EXISTENT);
        }
    }

    /**
     * 详情
     * @param idParam 请求参数
     * @return 信息
     */
    @Override
    public ProcessMaterials detail(ProcessMaterialsIdParam idParam) {
        return getInfo(idParam.getId());
    }

    /**
     * 分页查询
     * @param pageParam 请求参数
     * @return 分页信息
     */
    @Override
    public PageResult<ProcessMaterials> findPage(ProcessMaterialsPageParam pageParam) {
        LambdaQueryWrapper<ProcessMaterials> wrapper = new LambdaQueryWrapper<>();
        // 筛选主要属性
        /* wrapper.select(ProcessMaterials::getId, ProcessMaterials::getPositionName, ProcessMaterials::getCode, ProcessMaterials::getRemark,
        ProcessMaterials::getPositionSort, BaseEntity::getCreateTime);*/
        wrapper.eq(ProcessMaterials::getProductionProcessesId, pageParam.getProductionProcessesId());

        Page<ProcessMaterials> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    /**
     * 根据id获取信息
     * @param id id
     * @return 信息
     */
    private ProcessMaterials getInfo(Long id) {
        if(ObjectUtil.isEmpty(id)) {
            throw new ServiceException(ProcessMaterialsException.ID_NOT_EXIST);
        }
        ProcessMaterials processMaterials = this.getById(id);
        if(ObjectUtil.isEmpty(processMaterials)) {
            throw new ServiceException(ProcessMaterialsException.PROCESS_MATERIALS_NOT_EXIST);
        }
        return processMaterials;
    }

    /**
     * 根据id获取工序
     * @param id 工序id
     * @return 工序信息
     */
    private ProductionProcesses getProductionProcesses(Long id) {
        ProductionProcesses productionProcesses = productionProcessesMapper.selectById(id);
        if(ObjectUtil.isEmpty(productionProcesses)) {
            throw new ServiceException(ProcessMaterialsException.PRODUCTION_PROCESSES_NOT_EXIST);
        }
        return productionProcesses;
    }
}
